import mysql.connector
import yaml

# 读取配置文件
with open('./config/config.yaml', 'r') as file:
    config = yaml.safe_load(file)

host = config['MYSQL_DATABASE']['host']
port = config['MYSQL_DATABASE']['port']
username = config['MYSQL_DATABASE']['username']
password = config['MYSQL_DATABASE']['password']
database = config['MYSQL_DATABASE']['dbname']
charset = config['MYSQL_DATABASE']['charset']
pool_name = config['MYSQL_DATABASE']['pool_name']
pool_size = config['MYSQL_DATABASE']['pool_size']


def conn_db():
    # 连接池配置（企业级推荐）
    dbconfig = {
        "host": host,
        "user": username,
        "password": password,
        "database": database,
        "port": port,
        "charset": charset,
        "pool_name": pool_name,
        "pool_size": pool_size  # 连接池大小
    }

    # 创建连接池
    connection_pool = mysql.connector.pooling.MySQLConnectionPool(**dbconfig)

    # 获取连接
    conn = connection_pool.get_connection()
    cursor = conn.cursor(dictionary=True)
    return conn, cursor
